using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
using CsvCatcher.DataSchema;

namespace CsvCatcher
{
    public partial class CsvCatcherImpl: HunterPluginSDK.Interface.IHunterPlugin
    {
        #region memebers
        DataSchema.CatcherSettings _settingsObj;
        Setup.SettingsDAO _settdao;

        private string _settPath;
        private HunterPluginSDK.Interface.IProgressHandler _progressHandler;
        #endregion

        #region events
        void w_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            FileInfo fi = new FileInfo(e.FullPath);
            if (fi.Exists)
            {
                try
                {
                    foreach (CsvCatcher.DataSchema.Rule rule in _settingsObj.Rules)
                    {
                        string expr = rule.Pattern;
                        Regex varToken = new Regex(expr, RegexOptions.Singleline);
                        bool isM = varToken.IsMatch(fi.Name);
                        if (isM)
                        {
                            _progressHandler.WriteToOutput("CsvCatcher. File " + fi.FullName + " matched with rule " + rule.Name, HunterPluginSDK.LogPriority.INFO);
                            this.PerformOperations(fi, rule);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _progressHandler.LogException(ex);
                }
            }
            else
                _progressHandler.WriteToOutput("CsvCatcher. Can't find file " + fi.FullName, HunterPluginSDK.LogPriority.ERROR);
        }

        #endregion

        #region IHunterPlugin Members

        bool HunterPluginSDK.Interface.IHunterPlugin.Load(HunterPluginSDK.Interface.IProgressHandler ph)
        {
            try
            {
                _progressHandler = ph;
                _settdao = new CsvCatcher.Setup.SettingsDAO(_settingsObj);
                _settdao.Deserialize(ref _settingsObj);

                foreach (Folder folder in _settingsObj.Folders)
                {
                    System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(folder.Path);
                    if (di.Exists)
                        {
                            System.IO.FileSystemWatcher w = new System.IO.FileSystemWatcher();
                            w.Path = folder.Path;
                            w.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                            w.EnableRaisingEvents = true;
                            w.Created += new System.IO.FileSystemEventHandler(w_Created);
                            _progressHandler.WriteToOutput("CsvCatcher. Catch from " + di.FullName + " set properly", HunterPluginSDK.LogPriority.INFO);
                        }
                    else
                        _progressHandler.WriteToOutput("CsvCatcher. Can't catch from " + di.FullName, HunterPluginSDK.LogPriority.INFO);
                }
                return true;
            }
            catch
            {
                return false;
            }
            
        }


        void HunterPluginSDK.Interface.IHunterPlugin.DoSetup()
        {
            Setup.SetupDlg dlg = new CsvCatcher.Setup.SetupDlg(_settingsObj);
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _settdao.Serialize();
            }
        }


        HunterPluginSDK.EventTypes[] HunterPluginSDK.Interface.IHunterPlugin.SupportedEvents
        {
            get { return new HunterPluginSDK.EventTypes[]{HunterPluginSDK.EventTypes.Watcher}; }
        }

 
        string HunterPluginSDK.Interface.IHunterPlugin.PluginName
        {
            get { return "Csv file Catcher"; }
        }

        string HunterPluginSDK.Interface.IHunterPlugin.PluginDescription
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        string HunterPluginSDK.Interface.IHunterPlugin.PluginVersion
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        bool  HunterPluginSDK.Interface.IHunterPlugin.Enabled
        {
	        get { return true; }
        }


        #endregion


        #region object overrides
        public override string ToString()
        {
            return (this as HunterPluginSDK.Interface.IHunterPlugin).PluginName;
        }
        #endregion
    
}
}
